package myProject.nim;

public class NimGame2 extends Rule{
    private int length;
    private int[] storeArray;
    private Machine1 machine1;
    private Judge judge;
    private Machine1 machine2;
    NimGame2(){
        judge = new Judge();
        this.length = judge.getTotalSets();
        storeArray = new int[length+1];
        machine1 = new Machine1();
        machine2 = new Machine1();
    }
    public void start2()
    {
        System.out.println("-----------"+machine1.getName()+" VS "+machine2.getName()+"-----------");
        judge.GeneratedRandomQueue(storeArray,length,1,100);
        judge.showStore("生成石子队列为：",storeArray,length);
        do {
            int idx1, idx2, num1, num2;
            //机器1操作
            int xorSum1 = machine1.getXorSum(storeArray,length);
            if(xorSum1 == 0){
                idx1 = machine1.getRandomInt(1,length + 1);
                num1 = machine1.getRandomInt(1,storeArray[idx1-1]+1);
            }else{
                idx1 = machine1.getOptimalIdx(storeArray,length,xorSum1);
                num1 = machine1.getOptimalNum(storeArray,idx1,xorSum1);
            }
            judge.showOperation(machine1.getName(),idx1,num1);
            if (isRemoveStore(storeArray, idx1, num1, length)) {
                length--;
            }
            if(judge.isEnd(length,machine1.getName(),machine2.getName())){
                break;
            }
            judge.showStore("剩余石子：",storeArray,length);

            //机器2操作
            int xorSum2 = machine2.getXorSum(storeArray,length);
            if(xorSum2 == 0){
                idx2 = machine2.getRandomInt(1,length + 1);
                num2 = machine2.getRandomInt(1,storeArray[idx2-1]+1);
            }else{
                idx2 = machine2.getOptimalIdx(storeArray,length,xorSum2);
                num2 = machine2.getOptimalNum(storeArray,idx2,xorSum2);
            }
            judge.showOperation(machine2.getName(),idx2,num2);
            if (isRemoveStore(storeArray, idx2, num2, length)) {
                length--;
            }
            if(judge.isEnd(length,machine2.getName(),machine1.getName())){
                break;
            }
            judge.showStore("剩余石子：",storeArray,length);

        }while (length>0);
        System.out.println("结束");
    }

}
